package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.interfaces.OrderVinService;
import cn.huiyunche.base.service.mappers.SOrderVinMapper;
import cn.huiyunche.base.service.model.SOrderVin;
import cn.huiyunche.base.service.model.SOrderVinExample;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service

public class OrderVinServiceImpl implements OrderVinService {

    @Autowired
    private SOrderVinMapper vinMapper;

    @Override
    public BigDecimal calculateThePrice(long orderId) {
        if (orderId == 0) {
            throw new IllegalArgumentException("OrderDetialServiceImpl.calculateThePrice param can not be 0!");
        }
        SOrderVinExample example = new SOrderVinExample();
        example.createCriteria().andOrderIdEqualTo(orderId);
        List<SOrderVin> details = vinMapper.selectByExample(example);
        BigDecimal total = BigDecimal.ZERO.setScale(2);
        if (CollectionUtils.isNotEmpty(details)) {
            for (int i = 0; i < details.size(); i++) {
                total = total.add(details.get(i).getEstValue());
            }
        }
        return total;
    }

}
